#include <iostream>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        cout << "use error, " << argv[0] << " ip port" << endl;
    }

    uint16_t port = stoi(argv[2]);
    string ip = argv[1];

    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd == -1)
    {
        return 1;
    }

    struct sockaddr_in addr;
    bzero(&addr, sizeof(addr));
    socklen_t len = sizeof(addr);
    addr.sin_port = htons(port);
    addr.sin_family = AF_INET;
    inet_aton(ip.c_str(), &addr.sin_addr);

    while (true)
    {
        char buffer[1024];
        cin >> buffer;
        ssize_t ret = sendto(fd, buffer, sizeof(buffer), 0, (struct sockaddr *)&addr, len);

        if (ret > 0)
        {
            ret = recvfrom(fd, buffer, sizeof(buffer) - 1, 0, (struct sockaddr *)&addr, &len);
            buffer[ret] = 0;
            cout << "server say# " << buffer << endl;
        }
        else
        {
            continue;
        }
    }

    return 0;
}